home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / read_xwd.pro < prev    next >
Text File  |  1997-07-08  |  4KB  |  152 lines

  1. ; $Id: read_xwd.pro,v 1.4 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1990-1997, Research Systems, Inc.  All rights reserved.
  4. ;       Unauthorized reproduction prohibited.
  5.  
  6. FUNCTION READ_XWD, FILE_NAME, RED, GREEN, BLUE
  7. ;+
  8. ; NAME:
  9. ;    READ_XWD
  10. ;
  11. ; PURPOSE:
  12. ;    Read the contents of files created by the XWD (X Windows Dump)
  13. ;    command and return the image and color table vectors in the form of
  14. ;    IDL variables.
  15. ;
  16. ; CATEGORY:
  17. ;    Input/Output.
  18. ;
  19. ; CALL:
  20. ;    Result = READ_XWD(File_Name [, R, G, B])
  21. ;
  22. ; INPUTS:
  23. ;   File_Name:    Scalar string giving the name of the xwd file to read
  24. ;
  25. ; OUTPUTS:
  26. ;    READ_XWD returns a 2D byte array containing the image.  If the file
  27. ;    cannot be open or read, the return value is zero.
  28. ;
  29. ; OPTIONAL OUTPUT PARAMETERS:
  30. ;     R, G, B:    The variables to contain the Red, Green, and Blue color
  31. ;        vectors if the XWD file contains color tables.
  32. ;
  33. ; COMMON BLOCKS:
  34. ;    None.
  35. ;
  36. ; SIDE EFFECTS:
  37. ;    I/O is performed.
  38. ;
  39. ; RESTRICTIONS:
  40. ;    This function is intended to be used only on files containing
  41. ;    8-bit pixmaps.  It is not intended to be used with all XWD files.
  42. ;    No guarantees are made that all XWD files will work with this routine.
  43. ;    This routine will not work with XWD files with version less than 6.
  44. ;
  45. ; PROCEDURE:
  46. ;    The header is read into a structure and the bytes are reversed
  47. ;    if necessary.  Then the colormap and image portions of the
  48. ;    file are read into their respective variables.
  49. ;
  50. ; EXAMPLE:
  51. ;    To open and read the X Windows Dump file named "my.xwd" in the current
  52. ;    directory, store the image in the variable IMAGE1, and store the color 
  53. ;    vectors in the variables, R, G, and B, enter:
  54. ;
  55. ;        IMAGE1 = READ_XWD("my.xwd", R, G, B)
  56. ;
  57. ;    To load the new color table and display the image, enter:
  58. ;
  59. ;        TVLCT, R, G, B
  60. ;        TV, IMAGE1
  61. ;
  62. ;
  63. ; MODIFICATION HISTORY:
  64. ;    September, 1990 DMS and SMR, Research Systems, Inc.
  65. ;-
  66.  
  67.  
  68. on_error, 2                ; return to caller on error
  69. on_ioerror, error            ; goto error section on error
  70. openr,unit, FILE_NAME, /get_lun        ; open the file
  71.  
  72. hdr = { XWD_FILE_HEADER, $        ; definition of xwd header struct
  73.     header_size : 0L, $
  74.     file_version : 0L, $
  75.     pixmap_format: 0L, $
  76.     pixmap_depth: 0L, $
  77.     pixmap_width: 0L, $
  78.     pixmap_height: 0L, $
  79.     xoffset: 0L, $
  80.     byte_order: 0L, $
  81.     bitmap_unit: 0L, $
  82.     bitmap_bit_order: 0L, $
  83.     bitmap_pad: 0L, $
  84.     bits_per_pixel: 0L, $
  85.     bytes_per_line: 0L, $
  86.     visual_class: 0L, $
  87.     red_mask: 0L, $
  88.     green_mask: 0L, $
  89.     blue_mask: 0L, $
  90.     bits_per_rgb: 0L, $
  91.     colormap_entries: 0L, $
  92.     ncolors: 0L, $
  93.     window_width: 0L, $
  94.     window_height: 0L, $
  95.     window_x: 0L, $
  96.     window_y: 0L, $
  97.     window_bdrwidth: 0L }
  98.  
  99. Color = { XWDColor, $            ;The xwd color element structure
  100.     pixel: 0L, $
  101.     red: 0,$
  102.     green: 0, $
  103.     blue: 0, $
  104.     flags: 0B, $
  105.     pad: 0B }    
  106.  
  107. readu, unit, hdr            ; read the header
  108. test = 1L                ; do a test to check the system's byte
  109. byteorder, test, /htonl            ; order. If needed switch the byte 
  110. if (test ne 1L) then $            ; order to correspond to network byte
  111.    byteorder, hdr, /htonl        ; order
  112.  
  113. point_lun, unit, hdr.header_size    ; seek to beginning of colormap
  114.  
  115. colormap = replicate(color, hdr.ncolors)
  116. readu, unit, colormap            ; read the colormap entries from file
  117.  
  118. if hdr.pixmap_format ne 2 then begin
  119.     message,'READ_XWD: can only handle Z format pixmaps.'
  120.     endif
  121.  
  122.  
  123. IMAGE = bytarr(hdr.pixmap_width, hdr.pixmap_height)  ;Create the pixmap
  124. line = bytarr(hdr.bytes_per_line)        ; get one scan line
  125.  
  126. for i=0, hdr.pixmap_height-1 do begin    ;Read each scan line
  127.     readu, unit, line        
  128.     if hdr.pixmap_width eq hdr.bytes_per_line then $
  129.         IMAGE[0,hdr.pixmap_height - i -1] = line $
  130.     else image[0, hdr.pixmap_height - i - 1] = line[0:hdr.pixmap_width-1]
  131.     endfor
  132.  
  133. free_lun, unit                ; close and free the file
  134.  
  135. pixel = colormap.pixel            ; Extract pixel value separately
  136. if test ne 1L then begin        ; switch byte order if needed
  137.     byteorder, colormap, /htons    ; Swap whole structure
  138.     byteorder, pixel, /htonl    ; & pixel separately
  139.     endif
  140. RED = ishft(colormap.red, -8)
  141. GREEN = ishft(colormap.green, -8)
  142. BLUE = ishft(colormap.blue, -8)
  143.  
  144. return, IMAGE
  145.  
  146. ;------------ the error message ------------
  147.  
  148. error: message, "READ_XWD: Error reading file: " + FILE_NAME
  149. end
  150.  
  151.  
  152.